package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";

    @Autowired
    private IStatementService statementService;
    @Autowired
    private IStatementItemService statementItemService;

    @RequestMapping("/itemDetail")
    public String itemDetail(Long statementId, Model model){
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement",statement);
        if (statement.getStatus().equals(Statement.STATUS_CONSUME)) {
            return prefix + "consumeList";
        }else{
            return prefix + "paidList";
        }
    }
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery qo){
        return statementItemService.query(qo);
    }

    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> list){
        statementItemService.saveItems(list);
        return AjaxResult.success();
    }
    @RequestMapping("/pay")
    @ResponseBody
    public AjaxResult pay(Long statementId){
        statementItemService.pay(statementId);
        return AjaxResult.success();
    }
    @RequestMapping("/exportFile")
    public void exportFile(StatementItemQuery qo, HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition","attachment;filename=Consume.xlsx");
        Workbook workbook = statementItemService.exportFile(qo);
        workbook.write(response.getOutputStream());
    }
}
